#!/bin/sh
PRJNAME=MinorityReport
PATH="/bin:/sbin:/usr/bin:/usr/sbin:/opt/TrendMicro/$PRJNAME/bin"
export PATH

OPTIONS="-f -p /var/run/snmpd.pid -a -A"
OPTIONS_DEBUG="-Lf /dev/null"
RETVAL=0
prog="snmpd"
GLCFG=glcfg
GGCFGFILE=/etc/ggb.conf
IGSACFGFILE=/mr_etc/igsa.conf
SNMPCONF_DEF=/etc/snmp/snmpd.conf.default

start() {
	echo -n $"Starting $prog: "
	snmpd $OPTIONS $OPTIONS_DEBUG 
	RETVAL=$?
	echo
	return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
	killall snmpd
	RETVAL=$?
	echo
	return $RETVAL
}

reload(){
	echo -n $"Reloading $prog: "
	killall snmpd
	RETVAL=$?
	echo
	return $RETVAL
}

restart(){
	stop
	start
}

generate_conf(){
AGENT_ON=`$GLCFG $GGCFGFILE v snmp enable_agent`
SYS_LOC=`$GLCFG $GGCFGFILE v snmp sys_location`
SYS_CONTACT=`$GLCFG $GGCFGFILE v snmp sys_contact`
COMMU_NAME=`$GLCFG $GGCFGFILE v snmp community_name`
COMMU_NAME_C=`$GLCFG $GGCFGFILE c snmp community_name`
COMMU_NAME_C=`expr $COMMU_NAME_C - 1`
TRUST_NMS=`$GLCFG $GGCFGFILE v snmp trusted_nms`
TRUST_NMS_C=`$GLCFG $GGCFGFILE c snmp trusted_nms`
TRUST_NMS_C=`expr $TRUST_NMS_C - 1`
TRAP_ON=`$GLCFG $GGCFGFILE v snmp enable_trap`
TRAP_COMMU=`$GLCFG $GGCFGFILE v snmp trap_community`
TRAP_IP=`$GLCFG $GGCFGFILE v snmp trap_ip`
SECURITY=

#ln -fs /var/conf/snmpd.conf /etc/snmp/snmpd.conf

if [ "$TRAP_ON" = 'yes' -o "$TRAP_ON" = 'YES' ]
then
	TRAP_ON=''
else
	TRAP_ON='#'
fi

if [ "$SYS_LOC" = "" ]
then
	SYSLOC_ON=''
	SYS_LOC='""'
else
	SYSLOC_ON=''
fi

if [ "$SYS_CONTACT" = "" ]
then
	SYSCONTACT_ON=''
	SYS_CONTACT='""'
else
	SYSCONTACT_ON=''
fi

for i in `seq 0 $COMMU_NAME_C`
do
	# add workaround for amp;xxxxx
	COMMU_NAME_V=`$GLCFG $GGCFGFILE v snmp community_name $i | sed 's/^amp;\(.*\)/\1/g'`
	if [ "$COMMU_NAME_V" = "" ]
	then
		COMMU_NAME_V='pub'`date +%b%a`'lic'
	fi
	for j in `seq 0 $TRUST_NMS_C`
	do
		# add workaround for amp;xxxxx
		TRUST_NMS_V=`$GLCFG $GGCFGFILE v snmp trusted_nms $j | sed 's/^amp;\(.*\)/\1/g'`
		if [ "$TRUST_NMS_V" = "" ]
		then
			TRUST_NMS_V='127.0.0.1'
		fi
		SECURITY="$SECURITY\ncom2sec GGUser $TRUST_NMS_V $COMMU_NAME_V"
	done
done

if [ "$AGENT_ON" = 'yes' -o "$AGENT_ON" = 'YES' ]
then
	AGENT_ON='#'
else
	AGENT_ON=''
fi

sed -e "s/\$\$SECURITY/$SECURITY/g" -e "s/\$\$SYS_LOC/$SYS_LOC/g" -e "s/\$\$SYS_CONTACT/$SYS_CONTACT/g" -e "s/\$\$TRAP_ON/$TRAP_ON/g" -e "s/\$\$SYSLOC_ON/$SYSLOC_ON/g" -e "s/\$\$SYSCONTACT_ON/$SYSCONTACT_ON/g" -e "s/\$\$TRAP_IP/$TRAP_IP/g" -e "s/\$\$TRAP_COMMU/$TRAP_COMMU/g" -e "s/\$\$AGENT_ON/$AGENT_ON/g" $SNMPCONF_DEF > /etc/snmp/snmpd.conf

level=`$GLCFG $IGSACFGFILE val debug log_level`
case $level in
-1)
	OPTIONS_DEBUG="-Lf /dev/null"
	;;
0)
	OPTIONS_DEBUG="-LF 0 /var/log/snmpd.log -DALL"
	;;
1)
	OPTIONS_DEBUG="-LF 3 /var/log/snmpd.log -DALL"
	;;
2)
	OPTIONS_DEBUG="-LF 4 /var/log/snmpd.log -DALL"
	;;
3)
	OPTIONS_DEBUG="-LF 6 /var/log/snmpd.log -DALL"
	;;
4)
	OPTIONS_DEBUG="-LF 7 /var/log/snmpd.log -DALL -d"
	;;
*)
	OPTIONS_DEBUG="-Lf /dev/null"
	;;
esac
}

#ln -fs /var/conf/snmpd.conf /etc/snmp/snmpd.conf
ln -fs /etc/snmp/snmpd.conf /var/conf/snmpd.conf

generate_conf
case "$1" in
start)
	start
	;;
stop)
	stop
	;;
restart)
	restart
	;;
reload)
	if [ "$2" = 'daemon' ]
	then
		SNMP_ENABLE=`$GLCFG $GGCFGFILE v snmp enable`
		genDaemonTab
		if [ "$SNMP_ENABLE" = 'yes' ]
		then
			killall lazyMan
		else
			stop
		fi
	elif [ "$2" = 'config' ]
	then
		reload
	fi
	;;
*)
	echo $"Usage: $0 {start|stop|restart|reload daemon|reload config}"
	RETVAL=1
esac

exit $RETVAL
